home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 285_02 / output.c < prev    next >
Text File  |  1990-07-08  |  31KB  |  1,372 lines

  1. /* Output the generated parsing program for bison,
  2.    Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3.  
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. /* functions to output parsing data to various files.  Entries are:
  22.  
  23.   output_headers ()
  24.  
  25. Output constant strings to the beginning of certain files.
  26.  
  27.   output_trailers()
  28.  
  29. Output constant strings to the ends of certain files.
  30.  
  31.   output ()
  32.  
  33. Output the parsing tables and the parser code to ftable.
  34.  
  35. The parser tables consist of:  (starred ones needed only for the semantic parser)
  36.  
  37. yytranslate = vector mapping yylex's token numbers into bison's token numbers.
  38.  
  39. yytname = vector of string-names indexed by bison token number
  40.  
  41. yyrline = vector of line-numbers of all rules.  For yydebug printouts.
  42.  
  43. * yyrhs = vector of items of all rules.
  44.         This is exactly what ritems contains.
  45.  
  46. * yyprhs[r] = index in yyrhs of first item for rule r.
  47.  
  48. yyr1[r] = symbol number of symbol that rule r derives.
  49.  
  50. yyr2[r] = number of symbols composing right hand side of rule r.
  51.  
  52. * yystos[s] = the symbol number of the symbol that leads to state s.
  53.  
  54. yydefact[s] = default rule to reduce with in state s,
  55.           when yytable doesn't specify something else to do.
  56.           Zero means the default is an error.
  57.  
  58. yydefgoto[i] = default state to go to after a reduction of a rule that
  59.            generates variable ntokens + i, except when yytable
  60.            specifies something else to do.
  61.  
  62. yypact[s] = index in yytable of the portion describing state s.
  63.             The lookahed token's type is used to index that portion
  64.             to find out what to do.
  65.  
  66.         If the value in yytable is positive,
  67.         we shift the token and go to that state.
  68.  
  69.         If the value is negative, it is minus a rule number to reduce by.
  70.  
  71.         If the value is zero, the default action from yydefact[s] is used.
  72.  
  73. yypgoto[i] = the index in yytable of the portion describing 
  74.              what to do after reducing a rule that derives variable i + ntokens.
  75.              This portion is indexed by the parser state number
  76.          as of before the text for this nonterminal was read.
  77.          The value from yytable is the state to go to.
  78.  
  79. yytable = a vector filled with portions for different uses,
  80.           found via yypact and yypgoto.
  81.  
  82. yycheck = a vector indexed in parallel with yytable.
  83.       It indicates, in a roundabout way, the bounds of the
  84.       portion you are trying to examine.
  85.  
  86.       Suppose that the portion of yytable starts at index p
  87.       and the index to be examined within the portion is i.
  88.       Then if yycheck[p+i] != i, i is outside the bounds
  89.       of what is actually allocated, and the default
  90.       (from yydefact or yydefgoto) should be used.
  91.       Otherwise, yytable[p+i] should be used.
  92.  
  93. YYFINAL = the state number of the termination state.
  94. YYFLAG = most negative short int.  Used to flag ??
  95. YYNTBASE = ntokens.
  96.  
  97. */
  98.  
  99. #include <stdio.h>
  100. #include <stdlib.h>
  101. #include <ctype.h>
  102. #include <string.h>
  103.  
  104. #include "machine.h"
  105. #include "new.h"
  106. #include "files.h"
  107. #include "gram.h"
  108. #include "state.h"
  109. #include "bison.h"
  110. #include "output.h"
  111. #include "bison.h"
  112.  
  113. /* #define    MAXTABLE 32767 */
  114. /* 
  115.     Microsoft v5.1 variations:
  116.         malloc() gets 65516 bytes MAX in one object,
  117.         so MAXTABLE is constrained to 65516 / sizeof( short int))
  118.         = 32758 or less.
  119. */
  120. #define MAXTABLE 32700
  121.  
  122. #define DOSTRACK 4608           /* bytes per physical track in vanilla DOS */
  123.  
  124. extern int debugflag;
  125. extern int nolinesflag;
  126.  
  127. extern char **tags;
  128. extern int tokensetsize;
  129. extern int final_state;
  130. extern core **state_table;
  131. extern shifts **shift_table;
  132. extern errs **err_table;
  133. extern reductions **reduction_table;
  134. extern short *accessing_symbol;
  135. extern unsigned *LA;
  136. extern short *LAruleno;
  137. extern short *lookaheads;
  138. extern char *consistent;
  139. extern short *goto_map;
  140. extern short *from_state;
  141. extern short *to_state;
  142.  
  143.  
  144. static int nvectors;
  145. static int nentries;
  146. static short **froms;
  147. static short **tos;
  148. static short *tally;
  149. static short *width;
  150. static short *actrow;
  151. static short *state_count;
  152. static short *order;
  153. static short *base;
  154. static short *pos;
  155. static short *table;
  156. static short *check;
  157. static int lowzero;
  158. static int high;
  159.  
  160.  
  161. #define    GUARDSTR    "\n#include \"%s\"\nextern int yyerror;\n\
  162. extern int yycost;\nextern char * yymsg;\nextern YYSTYPE yyval;\n\n\
  163. yyguard(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
  164. register YYLTYPE *yylsp;\n\
  165. {\n  yyerror = 0;\nyycost = 0;\n  yymsg = 0;\nswitch (n)\n    {"
  166.  
  167. #define    ACTSTR        "\n#include \"%s\"\nextern YYSTYPE yyval;\
  168. \nextern int yychar;\
  169. yyaction(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
  170. register YYLTYPE *yylsp;\n{\n  switch (n)\n{"
  171.  
  172. #define    ACTSTR_SIMPLE    "\n  switch (yyn) {\n"
  173.  
  174.  
  175. void output_headers()
  176. {
  177.     if (semantic_parser)
  178.         fprintf(fguard, GUARDSTR, attrsfile);
  179.     fprintf(faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile);
  180.  
  181.     /*  if (semantic_parser)    JF moved this below
  182.     fprintf(ftable, "#include \"%s\"\n", attrsfile);
  183.   fprintf(ftable, "#include <stdio.h>\n\n"); */
  184. }
  185.  
  186. void output_trailers()
  187. {
  188.     if (semantic_parser)
  189.     {
  190.         fprintf(fguard, "\n    }\n}\n");
  191.         fprintf(faction, "\n    }\n}\n");
  192.     }
  193.     else
  194.         fprintf(faction, "\n}\n");
  195. }
  196.  
  197.  
  198. void output()
  199. {
  200.     int c;
  201.     /* output_token_defines(ftable);    /* JF put out token defines FIRST */
  202.  
  203.     if (!semantic_parser)        /* JF Put out other stuff */
  204.     {
  205.         rewind(fattrs);
  206.         while ((c=getc(fattrs))!=EOF)
  207.             putc(c,ftable);
  208.     }
  209.  
  210.     if (debugflag)
  211.         fprintf(ftable, "#define YYDEBUG\n");
  212.  
  213.     if (semantic_parser)
  214.         fprintf(ftable, "#include \"%s\"\n", attrsfile);
  215.     fprintf(ftable, "#include <stdio.h>\n\n");
  216.  
  217.     /* Make "const" do nothing if not in ANSI C.  */
  218.     fprintf (ftable, "#ifndef __STDC__\n#define const\n#endif\n\n");
  219.  
  220.     free_itemsets();
  221.     output_defines();
  222.     output_token_translations();
  223.     if (semantic_parser)
  224.         output_gram();
  225.     FREE(ritem);
  226.     if (semantic_parser)
  227.         output_stos();
  228.     output_rule_data();
  229.     output_actions();
  230.     output_parser();
  231.     output_program();
  232. }
  233.  
  234. void output_token_translations()
  235. {
  236.     register int i, j;
  237.     /*   register short *sp; JF unused */
  238.     if (translations)
  239.     {
  240.         fprintf(ftable,
  241.           "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
  242.           max_user_token_number, nsyms);
  243.  
  244.         if (ntokens < 127)  /* play it very safe; check maximum element value.  */
  245.             fprintf(ftable, "\nstatic const char yytranslate[] = {     0");
  246.         else
  247.             fprintf(ftable, "\nstatic const short yytranslate[] = {     0");
  248.  
  249.         j = 10;
  250.         for (i = 1; i <= max_user_token_number; i++)
  251.         {
  252.             putc(',', ftable);
  253.  
  254.             if (j >= 10)
  255.             {
  256.                 putc('\n', ftable);
  257.                 j = 1;
  258.             }
  259.             else
  260.             {
  261.                 j++;
  262.             }
  263.  
  264.             fprintf(ftable, "%4d", token_translations[i]);
  265.